﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace syc.Sale
{
    public partial class 物流旬报设置 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.NianFen.Text = DateTime.Today.Year.ToString();
                this.YueFen.Text = DateTime.Today.Month.ToString();
                this.QueryData();
                this.SqlDataSource1.SelectCommand = "select * from 物流旬报设置   order by 年度 desc, 月份 desc";
            }
            this.Message.Text = "·准备就绪";
        }
        private void QueryData()
        {
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;

            Cmd.CommandText = "select 月初日期,上旬日期,中旬日期,月末日期 from 物流旬报设置 where  年度=" + this.NianFen.Text + " and 月份=" + this.YueFen.Text;

            Cmd.CommandType = CommandType.Text;
            SqlDataReader Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                this.Date1.Text = ((DateTime)Reader[0]).ToLongDateString();
                this.Date2.Text = ((DateTime)Reader[1]).ToLongDateString();
                this.Date3.Text = ((DateTime)Reader[2]).ToLongDateString();
                this.Date4.Text = ((DateTime)Reader[3]).ToLongDateString();
            }
            else
            {
                this.Date1.Text = "";
                this.Date2.Text = "";
                this.Date3.Text = "";
                this.Date4.Text = "";
            }
            Reader.Close();
            Conn.Close();
            Reader.Dispose();
            Cmd.Dispose();
            Conn.Dispose();
        }
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            DateTime d1, d2, d3, d4;
            if (!DateTime.TryParse(Date1.Text, out d1))
            {
                this.Message.Text = "·月初日期录入有误";
                return;
            }
            if (!DateTime.TryParse(Date2.Text, out d2))
            {
                this.Message.Text = "·上旬截止日期录入有误";
                return;
            }
            if (!DateTime.TryParse(Date3.Text, out d3))
            {
                this.Message.Text = "·中旬截止日期录入有误";
                return;
            }
            if (!DateTime.TryParse(Date4.Text, out d4))
            {
                this.Message.Text = "·月末日期录入有误";
                return;
            }
            int nian, yue;
            nian = int.Parse(this.NianFen.Text);
            yue = int.Parse(this.YueFen.Text);
            string sql =
@"if EXISTS(select * from 物流旬报设置 where 年度=@年度 and 月份=@月份)
begin
update 物流旬报设置 set 月初日期=@d1,上旬日期=@d2,中旬日期=@d3,月末日期=@d4 where 年度=@年度 and 月份=@月份
end
else
begin
insert into 物流旬报设置 values (@年度,@月份,@d1,@d2,@d3,@d4)
end";

            sql = sql.Replace("@年度", this.NianFen.Text);
            sql = sql.Replace("@月份", this.YueFen.Text);
            sql = sql.Replace("@d1", "'" + d1.ToShortDateString() + "'");
            sql = sql.Replace("@d2", "'" + d2.ToShortDateString() + "'");
            sql = sql.Replace("@d3", "'" + d3.ToShortDateString() + "'");
            sql = sql.Replace("@d4", "'" + d4.ToShortDateString() + "'");
            this.SqlDataSource1.UpdateCommand = sql;
            this.SqlDataSource1.Update();
            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand = "select * from 物流旬报设置  order by 年度 desc, 月份 desc";
        }

        protected void NianFen_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.QueryData();
        }

        protected void YueFen_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.QueryData();
        }
    }
}
